﻿var paginationTotal = 100;//记录总数
var supervisor_id = 0;//上级部门ID
var isSearch = false;
//列出下级部门
function listSubDepartment(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    supervisor_id = id;
    isSearch = false;
    initData(0);
}
//列出上级部门
function listSuperDeparment() {
    isSearch = false;
    supervisor_id = 0;
    initData(0);
}

function submitsearch() {
    isSearch = true;
    //supervisor_id=0;
    paginationCurrentPage = 1;
    initData(0);
}
function loadData(page, pageSize) {
    if (supervisor_id == 0) {
        $('#superDepartment').hide();
    } else {
        $('#superDepartment').show();
    }
    var sname = "";
    if (isSearch) {
        sname = $("[name=sName]").val();
    }
    $.ajax({ //这里使用到Jquery的ajax方法
        type: "POST",
        async: false,
        dataType: "json",
        url: "/user/department/list", //请求的处理url
        data: "page=" + page + "&pageSize=" + pageSize + "&supervisor_id=" + supervisor_id + "&sname=" + sname,
        success: function(json) {
            var list = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='5' class=''>&nbsp;暂无数据</td>";
            } else {
                var i = paginationCurrentPage == 1 ? 1 : paginationPageSize * (paginationCurrentPage - 1) + 1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    list += "<td >" + (i++) + "</td>";
                    list += "<td >" + val.ud_name + "</td>";
                    list += "<td >" + val.ud_sort + "</td>";
                    list += "<td><a href=\"javascript:listSubDepartment(" + val.ud_id + ")\"><img src=\"/images/user/4.png\"></a</td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.ud_id + ")\"><img src=\"/images/icon/edit.gif\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.ud_id + ")\"><img src=\"/images/icon/delete.gif\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#list").html(list);
        }
    });
}

function loadSelect(id) {
    $.ajax({
        type: "get",
        async: false,
        dataType: "json",
        url: "/user/Department/select",
        data: "id=" + id,
        success: function(json) {
            var list = "<option value='0'>根目录</option>";
            var parent = '';
            if (json == "" || json == null) {
            } else {
                for (var i = 0; i < json.length; i++) {
                    if (json[i].ud_supervisor_id != 0) {
                        parent = "--";
                    }
                    list += "<option value=" + json[i].ud_id + ">" + parent + json[i].ud_name + "</option>";
                }
            }
            $("#supervisor_id").html(list);
        }
    });
}

//删除
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#department_info").dialog("close");
}

$(function() {
    initData(0);
    //loadSelect(0);
    $("#department_info").dialog({
        autoOpen:false,
        modal:true,
        width:360,
        show:"slide",
        buttons: {
            '确定': function() {
                create();
            },
            '取消': function() {
                tip('');
                $(this).dialog('close');
            }
        }
    });

    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '确定': function() {
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/user/department/delete",
                    data: "ud_id=" + Uid,
                    success: function(json) {
                        if (json.data == '1') {
                            loadData(paginationCurrentPage, paginationPageSize);
                            messags = "成功删除！";
                        } else {
                            messags = "删除失败！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '取消': function() {
                tip('');
                $(this).dialog('close');
            }
        }
    });

    $("#create").click(function() {
        loadSelect(0);
        tip('');
        $("#department_info").dialog("open");
        $("#createForm input").each(function() {
            $(this).val('');
        });
    });
    $("#close").click(function() {
        dialogclose();
    });
});
//修改、添加
function create() {
    departName = $("[name=departName]").val();
    supervisor_id = $("[name=supervisor_id]").val();
    departSort = $("[name=departSort]").val();
    departId = $("[name=departId]").val();
    var message = "";
    if (departName == '') {
        message += "<span><img src=\"/images/no.gif\">部门名称不能为空!</span>";
    }
    if (message != '') {
        tip(message);
    } else {
        $.ajax({
            type: "post",
            async: false,
            dataType: "json",
            url: "/user/department/create",
            data: "departName=" + departName + "&supervisor_id=" + supervisor_id + "&departSort=" + departSort + "&departId=" + departId,
            success: function(json) {
                if (json.ask != '0') {
                    if (json.ask == 1) {
                        initData(0);
                        alertTip("添加成功!");
                        dialogclose();
                    } else if (json.ask == 2) {
                        loadData(paginationCurrentPage, paginationPageSize);
                        alertTip("修改成功!");
                        dialogclose();
                    } else {
                        tip("你没有修改任何资料！");
                    }
                } else {
                    if (json.error == null)return;
                    var html = '';
                    $.each(json.error, function(key, val) {
                        html += '<span><img src="/images/no.gif">' + val + '</span>';
                    })
                    tip(html);
                }
            }
        });
    }
}

function tip(mess) {
    var tips = $("#tips");
    tips.html(mess);
    if (mess != '') {
        tips.show();
    } else {
        tips.hide();
    }
}

function edit(id) {
    tip('');
    loadSelect(0);
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/user/Department/info",
        data: "ud_id=" + id,
        success: function(json) {
            if (json != '' && json != null) {
                $("[name=departName]").val(json.ud_name);
                $("[name=supervisor_id]").val(json.ud_supervisor_id);
                $("[name=departSort]").val(json.ud_sort);
                $("[name=departId]").val(json.ud_id);
                $("#department_info").dialog("open");
            }
        }
    });
}

//操作提示
function alertTip(tip) {
    $('<div title="提示"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '确定': function() {
                $(this).dialog("close");
            }
        }
    });
}